//
//  SEGMissionRecordDetailViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/19.
//

#import "SEGMissionRecordDetailViewModel.h"

@interface SEGMissionRecordDetailViewModel ()
/**
 中奖记录ID
 */
@property (nonatomic, copy) NSString *winnerId;

@end

@implementation SEGMissionRecordDetailViewModel
#pragma mark - life cycle
- (instancetype)initWithWinnerId:(NSString *)winnerId {
    if (self = [super init]) {
        _winnerId = winnerId;
        [self seg_creatRecordDetailsCommand];
        [self seg_creatSubmitRechargeTelCommand];
        
    }
    return self;
}

#pragma mark - init


#pragma mark - send request
- (void)seg_creatRecordDetailsCommand {
    @weakify(self);
    self.recordDetailCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [subscriber sendNext:@(1)];
            [[SEGWinningrecorddetailManager sharedManager] requestWinningrecorddetailWithRecordid:self.winnerId finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                [subscriber sendNext:@(0)];
                if (isSuccessed) {
                    SEGWinningrecorddetailDataModel *data = [SEGWinningrecorddetailManager sharedManager].winningRecordDetail.data;
                    if (data.prizeType.length > 0) {//防止没有时崩溃
                        self.prizeType = [data.prizeType integerValue];
                        if(self.prizeType == SEGWinReordPrizeType_OnLine) {//线下活动 地址处理
                            if (data.winnerDetail.length > 0) {
                                NSDictionary *winnerDetail = [SEGWinningrecorddetailManager stringToJson:data.winnerDetail];
                                if (winnerDetail) {
                                    self.addressData.userAddress = data.address;
                                    self.addressData.contactName = data.addressee;
                                    self.addressData.contactPhone = data.wTel;
                                }
                            }
                        }
                        [subscriber sendCompleted];
                    }
                }else {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
            }];
            return nil;
        }];
    }];
}

- (void)seg_creatSubmitRechargeTelCommand {
    @weakify(self);
    self.submitRechargeTelCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString * input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            [params setValue:input forKey:@"tel"];
            NSString *paramsString = [params yy_modelToJSONString];
            [[SEGReceiveprizeManager sharedManager] requestReceiveprizeWithRecordid:self.winnerId winnerDetail:paramsString finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    [subscriber sendCompleted];
                }else {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
            }];
            return nil;
        }];
    }];
}

#pragma mark - custom delegate

#pragma mark - system delegate

#pragma mark - event response
- (NSDictionary *)getPrizeActionParams {
    NSDictionary *params = @{@"showType" : [NSString stringWithFormat:@"%lu",(unsigned long)AdressListViewControllerType_Order],
                             //@"delegate" : self,
                             @"addressSelected" : [self.addressData.id length] > 0 ? self.addressData.id : [SEGUtils ValueToString:[SEGProfileManager sharedManager].profile.addressId],
                             @"shouldShowConfirmView" : @"1",
                             @"winnerId" : self.winnerId,
                             @"channel" : @"2", // 添加入口选择地址的渠道类型。
                             };
    return params;
}
#pragma mark - private methods

#pragma mark - setter and getter
- (SEGActivityManager *)activityManager {
    if (!_activityManager) {
        _activityManager = [[SEGActivityManager alloc] init];
    }
    return _activityManager;
}

- (SEGAddressModel*)addressData {
    if (!_addressData) {
        _addressData = [[SEGAddressModel alloc] init];
    }
    return _addressData;
}
@end
